package com.example.dbdesign.controller;

import com.example.dbdesign.mapper.UserMapper;
import com.example.dbdesign.pojo.entity.Result;
import com.example.dbdesign.pojo.entity.User;
import com.example.dbdesign.pojo.vo.LoginUser;
import com.example.dbdesign.pojo.vo.Res;
import com.example.dbdesign.service.ResultService;
import io.netty.util.internal.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/result")
public class ResultController {

    @Autowired
    private ResultService resultService;
    @Autowired
    private UserMapper userMapper;

    @PostMapping("/add")
    public Res add(@RequestBody Result result) {
        LoginUser lu = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        result.setId(lu.getUser().getId());
        Result result1 = resultService.add(result);
        if(result1 == null) {
            return Res.fail("添加失败");
        }else{
            return Res.success(result1);
        }
    }

    @GetMapping("/detail")
    public Res detail(@RequestParam("id") int id) {
        Result r = resultService.selectById(id);
        if(r == null) {
            return Res.fail("无效信息");
        }else{
            return Res.success(r);
        }
    }

    @DeleteMapping("/del")
    public Res del(@RequestParam("id") int id) {
        Boolean boo = resultService.deleteById(id);
        if(boo) {
            return Res.success("删除成功");
        }else{
            return Res.fail("删除失败");
        }
    }

    @PostMapping("/review")
    public Res review(@RequestBody Result result) {
        Result r = (Result) resultService.selectById(result.getId());
        if(r.getReview() > 0){
            return Res.fail("不可更改");
        }
        int master = result.getMaster();
        User masterIsAvail = userMapper.queryById(master);
        System.out.println(masterIsAvail + "用户");
        if(masterIsAvail == null){
            return Res.fail("指导老师不存在");
        }
        Result r2 = resultService.review(result);
        if(r2 == null){
            return Res.fail("审查失败");
        }else{
            return Res.success(r2);
        }
    }

    @PostMapping("/update")
    public Res update(@RequestBody Result result) {
        Result r = (Result) resultService.selectById(result.getId());
        if(r.getReview() > 0){
            return Res.fail("不可更改");
        }
        int master = result.getMaster();
        User masterIsAvail = userMapper.queryById(master);
        System.out.println(masterIsAvail + "用户");
        if(masterIsAvail == null || masterIsAvail.getRole() != 2){
            return Res.fail("指导老师不存在");
        }
        Result r2 = resultService.update(result);
        if(r2 == null){
            return Res.fail("更改失败");
        }else{
            return Res.success(r2);
        }
    }


    @GetMapping("/list")
    public Res<List<Result>> list(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("page", (pageNum - 1) * pageSize);
        map.put("size", pageSize);
        List<Result> ans = resultService.selectByPage(map);
        return Res.success(ans);
    }

    @GetMapping("/pages")
    public Res caulate(@RequestParam int size) {
        return Res.success((resultService.calSum() + size - 1) / size);
    }


    @GetMapping("/getByTypeAndDate")
    public Res<List<Result>> getByTypeAndDate(@RequestParam int type, @RequestParam int date) {
        return Res.success(resultService.selectByTypeAndDate(type, date));
    }

    @GetMapping("/getForTeacher")
    public Res<List<Result>> getForTeacher() {
        List<Result> re = resultService.selectByTeacher();
        if(re != null){
            return Res.success(re);
        }else{
            return Res.fail("请求错误");
        }
    }

    @GetMapping("getForUser")
    public Res<List<Result>> getForUser() {
        List<Result> re = resultService.selectByStu();
        if(re != null){
            return Res.success(re);
        }else{
            return Res.fail("请求错误");
        }
    }
}
